//
// Author: Vladimir Migashko <migashko@gmail.com>, (C) 2011
//
// Copyright: See COPYING file that comes with this distribution
//

#ifndef FASYS_LIBS_CHANNEL_CLIENT_CHANNEL_CONNECTION_HPP
#define FASYS_LIBS_CHANNEL_CLIENT_CHANNEL_CONNECTION_HPP

#include <fas/inet/connection.hpp>
#include "channels/messages_channels.hpp"
#include "iclient_callback.hpp"
// #include "ichannel_server.hpp"

namespace fasys{ namespace client{ namespace channel{

namespace ai = ::fas::inet;

template<typename A, typename F>
class connection
 : public ai::mux_connection_base<A, F>
{
public:
  typedef ai::mux_connection_base<A, F> super;

  template<typename T>
  bool _release(T& t)
  {
    return super::_release(t);
  }

  iclient_callback* callback;

  connection()
    : callback(0)
  {}

  void incoming(message_notify& cmd)
  {
    if (callback)
      callback->process(cmd);
    /*
    if ( channels )
    {
      if ( channels->publish(message) )
      {
        if (server)
        {
          std::cout << "incoming message"<< std::endl;
          server->process(message);
        }
      }
      else
      {
      }
    }
    */
  }

  void incoming(subscribe_response& cmd)
  {
    if (callback)
      callback->process(cmd);
  }

  void outgoing(const message_publish& cmd)
  {
    super::get_aspect().template get<_message_>().notify(*this, cmd);
  }

  void outgoing(const subscribe_request& cmd)
  {
    super::get_aspect().template get<_subscribe_>().notify(*this, cmd);
  }

};

}}}

#endif
